package no.saiboten.milkywave.controller;

import java.util.List;

import no.saiboten.milkywave.entity.Event;
import no.saiboten.milkywave.service.EventService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

@Controller
public class EventController {

	EventService eventService;

	@Autowired
	public EventController(EventService eventService) {
		this.eventService = eventService;
	}

	@RequestMapping("/event/join/{eventid}/{key}")
	public RedirectView joinEvent(@PathVariable("eventid") Long eventid,
			@PathVariable("key") String key) {
		eventService.joinEvent(eventid, key);
		return new RedirectView("/event/view/" + eventid);
	}

	@RequestMapping("/event/view")
	public ModelAndView seeAllEvents() {
		List<Event> pastEvents = eventService.getEventsInThePast();

		Event currentEvent = eventService.getCurrentEvent();

		ModelAndView mav = new ModelAndView("events");
		mav.addObject("currentEvent", currentEvent);
		mav.addObject("pastEvents", pastEvents);
		return mav;
	}

	@RequestMapping("/event/view/{eventid}")
	public ModelAndView joinEvent(@PathVariable("eventid") Long eventid) {
		Event event = eventService.getEvent(eventid);
		ModelAndView mav = new ModelAndView("event");
		mav.addObject("event", event);
		return mav;
	}

}
